//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
	  data:{
		  "endDate":"结束日期",
		  "title":"采购活动标题",
		  goodsList:[
        {'goodsName':'物资名称(含单位)','goodsPrice':12,'goodsCount':1},
        {'goodsName':'物资名称(含单位)','goodsPrice':12,'goodsCount':1},
        {'goodsName':'物资名称(含单位)','goodsPrice':12,'goodsCount': 1}],
		  "wechat":"保留项，微信号码",
		  "wechatImg":"保留项，微信二维码",
		  "phone":"123",
		  "areaList":[{'areaName':'小区名字','areaStatus':1}]
	  },
	  "attribute":{
		  "editStatus":true //是否可以编辑，只有发起者可以编辑，其他只可参与
	  },

	  userInfo: {},
	  hasUserInfo: false,
	  canIUse: wx.canIUse('button.open-type.getUserInfo'),
	  height:'300'
  },
  onLoad: function () {
	  const pages = getCurrentPages()
	  console.log(pages,'page')
	  const id = pages[pages.length-1].options.id
	  if(id){
		this.getDetail(id) //如果不是新增按钮
	  }
      this.getNickname()
 	  this.getListHeight()
  },
  inputSave(e){
	  const value = e.detail.value
	  const key = e.currentTarget.dataset.key
	  this.setData({[key]:value})
	  console.log(e,'eee')
	  
  },
  goback(){
	  wx.navigateBack({
	    delta: 1//直接返回首页
	  })
  },
  getDetail(id){
	  const that =this
	  app && app.wxRequest(
	    '/order/info',
	  	{id},
		function(res){
			that.setData({data:res.data,...res.attribute});
		},
	  )
	  // wx.request({
	  //   url: 'https://wuhan2020.m1c.cn/order/info',
	  //   method: "get",
	  //   data: {
	  //     id
	  //   },
	  //   success: function (res) {
	  //     console.log(res,'data');
	  // 	 that.setData({data:res.data,...res.attribute});
	  
	  //   }
	  // })
  },
  save(){
	  const that=this
	  app && app.wxRequest(
	    '/order/save',
		{...this.data.data},
		function(res){
			that.goback()
		},
		'post')
	  // const that=this
	  // wx.request({
	  //   url: 'https://wuhan2020.m1c.cn/order/save',
	  //   method: "post",
	  //   data: {
	  //    ...this.data.data
	  //   },
	  //   success: function (res) {
	  //     console.log(res,'data');
	  //   }
	  // })
  },
  makeCall(){
	  console.log(this,'this')
	  if(this.data.attribute.editStatus) return
	  wx.makePhoneCall({
	    phoneNumber:this.data.data.phone//仅为示例，并非真实的电话号码
	  })
  },
  addList(){
	this.setData({'data.goodsList':[{'goodsName':'','goodsPrice':'','goodsCount':''}].concat(...this.data.data.goodsList)})
  },
  deleteList(e){
	   console.log(e,'i')
	   let index=e.currentTarget.dataset.index
	   let arr = this.data.data.goodsList
	   arr.splice(index,1)
	   this.setData({'data.goodsList':arr})
  },
  getListHeight(){
	  const that =this
	  let top = 0
	  const query = wx.createSelectorQuery()
	  query.select('#title').boundingClientRect()
	  query.exec(function(res){
	    console.log(res,'top')       // #the-id节点的上边界坐标
		top = res[0].top
	  })
	  wx.getSystemInfo({
		  success (res) {
			 that.setData({
				 height:res.windowHeight -top -20
			 })
		    }
	  })
  },
  getLocation(){
	   //if(!this.data.attribute.editStatus) return //不能编辑
	  const that =this
	  wx.getLocation({
	   type: 'gcj02', //返回可以用于wx.openLocation的经纬度
	   success (res) {
	     const latitude = res.latitude
	     const longitude = res.longitude
	     // wx.openLocation({
	     //   latitude,
	     //   longitude,
	     //   scale: 18
	     // })
		 wx.chooseLocation({
			 latitude,
			 longitude,
		       success:(res)=>{
		        console.log(res,'loc')
				 that.setData({
					 'data.areaList':[{'areaName':res.address,'areaStatus':0}].concat(...that.data.data.areaList)
				 })
		       }
		     })

	   }
	  })
  },
  getNickname(){
	  
	if (app.globalData.userInfo) {
	  this.setData({
		userInfo: app.globalData.userInfo,
		hasUserInfo: true
	  })
	} else if (this.data.canIUse){
	  // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
	  // 所以此处加入 callback 以防止这种情况
	  app.userInfoReadyCallback = res => {
		this.setData({
		  userInfo: res.userInfo,
		  hasUserInfo: true
		})
	  }
	} else {
	  // 在没有 open-type=getUserInfo 版本的兼容处理
	  wx.getUserInfo({
		success: res => {
		  app.globalData.userInfo = res.userInfo
		  this.setData({
			userInfo: res.userInfo,
			hasUserInfo: true
		  })
		}
	  })
	}
  },
  
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  
 
})
